﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TouchUI;

namespace MobileStore.WinPhone
{
    public partial class MainForm : Form
    {
        #region Declarations
        private List<Dialog> dialogStack;
        private Timer timer;
        private bool loaded = false;
        private int animationLeft;
        private int animationOffset;
        #endregion

        #region Contructor
        public MainForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Menu = null;
            this.WindowState = FormWindowState.Maximized;
            this.Show();
            this.Refresh();

            dialogStack = new List<Dialog>();
            MainDialog mainDialog = new MainDialog(Common.Values.ScreenFactor, Common.Values.ClientRectangle);
            mainDialog.Selected += new EventHandler(dialogSelected);
            dialogStack.Add(mainDialog);
            navigate();

            timer = new Timer();
            timer.Interval = 80; // 12.5 fps
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;

            loaded = true;

            this.Menu = mainMenu;
            this.WindowState = FormWindowState.Normal;
        }
        #endregion

        #region Activated / Deactivate
        private void MainForm_Activated(object sender, EventArgs e)
        {
            if(loaded)
                timer.Enabled = true;
        }

        private void MainForm_Deactivate(object sender, EventArgs e)
        {
            if(loaded)
                timer.Enabled = false;
        }
        #endregion

        #region Paint
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if(!loaded)
            {
                e.Graphics.DrawImage(Common.Values.DefaultBitmap, Screen.PrimaryScreen.Bounds, Screen.PrimaryScreen.Bounds, GraphicsUnit.Pixel);
                return;
            }

            dialogStack.Last().Paint(Common.Values.PaintGraphics);
            Rectangle r = Common.Values.ClientRectangle;
            if(animationLeft != 0)
            {
                e.Graphics.DrawImage(Common.Values.PaintCopyBitmap,
                    new Rectangle(animationLeft - Common.Values.ClientRectangle.Width * animationLeft.CompareTo(0), 0, r.Width, r.Height),
                    r, GraphicsUnit.Pixel);
                e.Graphics.DrawImage(Common.Values.PaintBitmap, new Rectangle(animationLeft, 0, r.Width, r.Height), r, GraphicsUnit.Pixel);
                animationLeft -= animationOffset;
                if(Math.Abs(animationLeft) < Math.Abs(animationOffset))
                    animationLeft = 0;
            }
            else
                e.Graphics.DrawImage(Common.Values.PaintBitmap, r, r, GraphicsUnit.Pixel);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }
        #endregion

        #region Event Handlers
        private void dialogSelected(object sender, EventArgs e)
        {
            this.Refresh();
            navigateForward();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            dialogStack.Last().DoAutomaticMotion();
            this.Refresh();
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            dialogStack.Last().MouseDown(new Point(e.X, e.Y));
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            dialogStack.Last().MouseMove(new Point(e.X, e.Y));
        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            dialogStack.Last().MouseUp(new Point(e.X, e.Y));
        }
        #endregion

        #region Navigation
        private void navigateForward()
        {
            timer.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            Common.Values.PaintCopyGraphics.DrawImage(Common.Values.PaintBitmap, 0, 0);
            
            if(dialogStack.Last() is MainDialog)
            {
                MainDialog mainDialog = (MainDialog)dialogStack.Last();
                Car selectedCar = mainDialog.Cars[mainDialog.SelectedLine];
                DetailDialog detailDialog = new DetailDialog(Common.Values.ScreenFactor, Common.Values.ClientRectangle, selectedCar);
                dialogStack.Add(detailDialog);
                navigate();
            }

            animationOffset = Common.Values.ClientRectangle.Width / 4;
            animationLeft = Common.Values.ClientRectangle.Width;
            Cursor.Current = Cursors.Default;
            timer.Enabled = true;
        }

        private void navigateBack()
        {
            timer.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            Common.Values.PaintCopyGraphics.DrawImage(Common.Values.PaintBitmap, 0, 0);

            dialogStack.Remove(dialogStack.Last());
            navigate();

            animationOffset = -Common.Values.ClientRectangle.Width / 4;
            animationLeft = -Common.Values.ClientRectangle.Width;
            Cursor.Current = Cursors.Default;
            timer.Enabled = true;
        }

        private void navigate()
        {
            if(dialogStack.Last() is MainDialog)
                actionMenuItem.Text = Strings.Select;
            if(dialogStack.Last() is DetailDialog)
                actionMenuItem.Text = Strings.Back;
            if(dialogStack.Last() is AboutDialog)
                actionMenuItem.Text = Strings.Cars;

            if(!Common.Values.TouchDevice)
            {
                if(dialogStack.Last() is MainDialog)
                    this.Text = Strings.AppTitle;
                else
                    this.Text = dialogStack.Last().Text;
            }
        }
        #endregion

        #region Resize
        private void MainForm_Resize(object sender, EventArgs e)
        {
            Common.Values.ClientRectangle = Screen.PrimaryScreen.WorkingArea;
            if(loaded)
                foreach(Dialog dialog in dialogStack)
                    dialog.Resize(Common.Values.ClientRectangle);
            this.Refresh();
        }
        #endregion

        #region Keyboard support
        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Back && actionMenuItem.Text == Strings.Back)
                navigateBack();
            else
                dialogStack.Last().KeyUp(e);
        }

        private void comboBox_KeyDown(object sender, KeyEventArgs e)
        {
            dialogStack.Last().KeyDown(e);
        }
        #endregion

        #region Menu options
        private void actionMenuItem_Click(object sender, EventArgs e)
        {
            if(actionMenuItem.Text == Strings.Select)
                navigateForward();
            else if(actionMenuItem.Text == Strings.Back)
                navigateBack();
            else if(actionMenuItem.Text == Strings.Cars)
            {
                dialogStack.Remove(dialogStack.Last());
                navigate();
            }
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            while(dialogStack.Count > 1)
                dialogStack.Remove(dialogStack.Last());
            AboutDialog aboutDialog = new AboutDialog(Common.Values.ScreenFactor, Common.Values.ClientRectangle);
            dialogStack.Add(aboutDialog);
            navigate();
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}